/**
 * MusicListFragment.java  [V 1.0.0]
 * classpath : com.example.learnservice.MusicListFragment
 * zhangyw Create at 2015年4月9日 下午3:02:40
 */
package com.example.learnservice;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.List;

import com.zyw.utils.IOUtils;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * com.example.learnservice.MusicListFragment
 * @author zhangyw
 * create at 2015年4月9日 下午3:02:40
 */
public class MusicListFragment extends Fragment{
  private static final String TAG = "MusicListFragment";
  private ListView musicListView;
  private Button stopBtn;
  private ListAdapter adapter;
  private List<File> dataList;
  
  /**
   * 
   */
  public MusicListFragment() {
  }

  /* (non-Javadoc)
   * @see android.support.v4.app.Fragment#onCreate(android.os.Bundle)
   */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FileFilter fileFilter=new FileFilter() {
      
      @Override
      public boolean accept(File pathname) {
        return IOUtils.isAduio(pathname.getName());
      }
    };
    dataList=IOUtils.getFilesFromDir(IOUtils.getExteralStorageDir(), fileFilter);
    adapter=new AduioListAdapter(getActivity(), dataList);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_main, container, false);
  }
  
  /* (non-Javadoc)
   * @see android.support.v4.app.Fragment#onViewCreated(android.view.View, android.os.Bundle)
   */
  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    musicListView=(ListView) view.findViewById(R.id.list_view_music);
    musicListView.setAdapter(adapter);
    musicListView.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent i = new Intent(MusicService.PLAY_ADUIO);
//        i.putExtra(MusicService.PLAY_ADUIO, Uri.fromFile(dataList.get(position)).toString());
        i.setData(Uri.fromFile(dataList.get(position)));
        i.setClass(getActivity(), MusicService.class);
        getActivity().startService(i);
      }
    });
    
    stopBtn=(Button) view.findViewById(R.id.stop_btn);
    stopBtn.setOnClickListener(new OnClickListener() {
      
      @Override
      public void onClick(View v) {
        Log.v("note", "on click");
        Intent i = new Intent(MusicService.STOP_IT);
        i.setClass(getActivity(), MusicService.class);
        getActivity().stopService(i);
      }
    });
    
  }
}
